package com.jt.controller;

import com.jt.service.FileService;
import com.jt.vo.ImageVO;
import com.jt.vo.SysResult;
import jdk.nashorn.internal.runtime.regexp.joni.Regex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;


@RestController
@CrossOrigin
@RequestMapping("/file")
public class FileController {

    //@PostMapping("/upload")
    public SysResult upload(MultipartFile file) throws IOException {
        //获取文件的名称
        String fileName = file.getOriginalFilename();
        //准备文件的目录
        String dir="E:/图片+壁纸/images";
        File dirFile = new File(dir);
        if (!dirFile.exists()){//判断目录是否存在
            dirFile.mkdir();//创建多级目录
        }
        String path = dir+"/"+fileName;
        //实现文件的上传
        file.transferTo(new File(path));
        return SysResult.success();
    }

    @Autowired
    private FileService fileService;

    @PostMapping("/upload")
    public SysResult fileUpload(MultipartFile file) throws IOException {

        ImageVO imageVO = fileService.upload(file);
        if (imageVO == null){
            return SysResult.fail();
        }
        return SysResult.success(imageVO);
    }
}
